package naojump.android.twicca.plugindriver;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

/**
 * twicca plug-in driver
 * 
 * @author Naoto Murata
 * @version 1
 */
public class NTwiccaPluginDriver extends Activity {

	/** Debug */
	private static boolean DEBUG = false;

	/** TAG */
	private String TAG = "twicca plug-in driver";

	/** intent action from twicca */
	private static String TWICCA_ACTION_UPLOAD = "jp.r246.twicca.ACTION_UPLOAD";

	/** Request code: start media chooser */
	private static final int RC_START_MEDIA_CHOOSER = 1;

	/** Request code: start plug-in */
	private static final int RC_START_PLUGIN = 2;

	/** btSelPhoto */
	private Button btSelPhoto;

	/** btSelVideo */
	private Button btSelVideo;

	/** txMedirUrl */
	private TextView txMedirUrl;

	/** btFinish */
	private Button btFinish;

	/**
	 * Handles btFinish on click.
	 */
	private void handleBtFinishOnClick() {
		if (DEBUG)
			Log.d(TAG, "handleBtFinishOnClick");

		finish();
	}

	/**
	 * Handles btSelPhoto on click.
	 */
	private void handleBtSelPhotoOnClick() {
		if (DEBUG)
			Log.d(TAG, "handleBtSelPhotoOnClick");

		showMediaChooser(btSelPhoto.getText().toString(), "image/*");
	}

	/**
	 * Handles btSelVideo on click.
	 */
	private void handleBtSelVideoOnClick() {
		if (DEBUG)
			Log.d(TAG, "handleBtSelVideoOnClick");

		showMediaChooser(btSelVideo.getText().toString(), "video/*");
	}

	/**
	 * Method for standalone testing application.
	 * 
	 * @see android.app.Activity#onActivityResult(int,int,Intent)
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (DEBUG)
			Log.d(TAG, "onActivityResult");

		if (resultCode == RESULT_OK) {
			if (requestCode == RC_START_MEDIA_CHOOSER && data != null) {

				// When result is from media chooser, start twicca plug-in.
				Intent newIntent = new Intent();
				newIntent.setAction(TWICCA_ACTION_UPLOAD);
				String type = getContentResolver().getType(data.getData());
				newIntent.setDataAndType(data.getData(), type);

				startActivityForResult(newIntent, RC_START_PLUGIN);
			} else if (requestCode == RC_START_PLUGIN && data != null
					&& data.getData() != null) {

				// When result is from plug-in, show URL.
				txMedirUrl.setText(data.getData().toString());
			}
		}
	}

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (DEBUG)
			Log.d(TAG, "onCreate");

		setContentView(R.layout.main);
		btSelPhoto = (Button) findViewById(R.id.select_photo);
		btSelPhoto.setOnClickListener(new View.OnClickListener() {
			@SuppressWarnings("synthetic-access")
			@Override
			public void onClick(View v) {
				handleBtSelPhotoOnClick();
			}
		});
		btSelVideo = (Button) findViewById(R.id.select_video);
		btSelVideo.setOnClickListener(new View.OnClickListener() {
			@SuppressWarnings("synthetic-access")
			@Override
			public void onClick(View v) {
				handleBtSelVideoOnClick();
			}
		});
		txMedirUrl = (TextView) findViewById(R.id.media_url);
		btFinish = (Button) findViewById(R.id.finish);
		btFinish.setOnClickListener(new View.OnClickListener() {
			@SuppressWarnings("synthetic-access")
			@Override
			public void onClick(View v) {
				handleBtFinishOnClick();
			}
		});

	} // onCreate

	/**
	 * Shows media chooser.
	 * 
	 * @param title
	 *            Title of the chooser
	 * @param type
	 *            Media type (e.g. "image/*")
	 */
	private void showMediaChooser(String title, String type) {
		if (DEBUG)
			Log.d(TAG, "showMediaChooser");

		Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
		intent.setType(type);
		startActivityForResult(Intent.createChooser(intent, title),
				RC_START_MEDIA_CHOOSER);
	}

}
